package utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class 日期格式进行取零操作 {

    public static void main(String[] args) {
        String startDateStr = "2024-11-01";
        String endDateStr = "2024-11-08";

        String formattedDateRange = formatDateRange(startDateStr, endDateStr);
        System.out.println(formattedDateRange); // 输出: 2024年11月1日-11月8日

        startDateStr = "2024-11-01";
        endDateStr = "2025-01-08";

        formattedDateRange = formatDateRange(startDateStr, endDateStr);
        System.out.println(formattedDateRange); // 输出: 2024年11月1日-2025年1月8日
    }

    public static String formatDateRange(String startDateStr, String endDateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");

        String startYear = startDate.getYear() + "年";
        String endYear = endDate.getYear() + "年";

        String startMonthDay = startDate.format(DateTimeFormatter.ofPattern("MM月dd日"));
        String endMonthDay = endDate.format(DateTimeFormatter.ofPattern("MM月dd日"));

        if (startDate.getYear() != endDate.getYear()) {
            return startYear + startMonthDay + "-" + endYear + endMonthDay;
        } else if (startDate.getMonthValue() != endDate.getMonthValue()) {
            return startYear + startMonthDay + "-" + endMonthDay;
        } else {
            return startYear + startMonthDay + "-" + endDate.getDayOfMonth() + "日";
        }
    }

}
